<?php


namespace Xin\ThinkPHP\Validate;

use think\Validate;
use Xin\ThinkPHP\Foundation\ServiceProvider;

class ValidateServiceProvider extends ServiceProvider
{

    /**
     * @inheritDoc
     */
    public function register()
    {
        Validate::maker(static function (Validate $v) {
            $v->extend('alphaDash2', function ($value) use ($v) {
                return $v->regex($value, '/^[A-Za-z0-9\.\-\_\\\]+$/');
            }, ':attribute只能是字母、数字和下划线_破折号-反斜杠\\');

            $v->extend('chsDash2', function ($value) use ($v) {
                return $v->regex($value, '/^[A-Za-z0-9\-\_\\\]+$/');
            }, ':attribute只能是汉字、字母、数字和下划线_破折号-反斜杠\\');

            $checkMobile = function ($value) use ($v) {
                return $v->regex($value, '/^1\d{10}+$/');
            };
            $v->extend('phone', $checkMobile, ':attribute不是一个合法的手机号');
            $v->extend('mobile', $checkMobile, ':attribute不是一个合法的手机号');

            $v->extend('password', function ($value) use ($v) {
                return $v->regex($value, '/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{6,16}/');
            }, ':attribute无效');

            $v->extend('nullable', function () {
                return true;
            });
        });
    }

}
